package com.boge.flow;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class FlowableTest3Assignee {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/01-base/Example01.bpmn20.xml")
                .name("任务分配案例")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());//215d67e9-8551-11f0-b62f-eeae520ef490
    }

    @Test
    void startFlow() {
        //流程定义表中动态维护的 act_re_procdef=>id
        String processDefinitionId = "Example01:1:21b524dc-8551-11f0-b62f-eeae520ef490";
        //我们创建流程图的时候自定义的。注意保证唯一  act_re_procdef=>key
        String processKey = "Example01";
        //根据流程定义id启动流程(推荐)
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId);
        //根据流程定义key启动流程
        //runtimeService.startProcessInstanceByKey(processKey);
    }

    /**
     * 根据用户查询待办信息
     */
    @Test
    void finishFlow() {
        //获取act_ru_task中assignee=zhangsan
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("zhangsan") //指定查询的条件
                .list();

        for (Task task : taskList) {
            System.out.println("task.getId() = " + task.getId());//d77a7b73-8558-11f0-bdb4-a26608a26c0a
        }
    }

    /**
     * 任务审批
     */
    @Test
    void completeTask() {
//        taskService.complete("d77a7b73-8558-11f0-bdb4-a26608a26c0a");//审批用户1 zhangsan Unknown property used in expression: ${myAssign1}
//        Map<String, Object> variables = new HashMap<>();
//        variables.put("myAssign1", "lisi");
//        taskService.complete("d77a7b73-8558-11f0-bdb4-a26608a26c0a",variables);//审批用户1 zhangsan

//        taskService.complete("8dcfba6d-8559-11f0-9e6e-a26608a26c0a");//审批用户2 lisi Unknown property used in expression: ${myBean.getAssignee()}
//        taskService.complete("8dcfba6d-8559-11f0-9e6e-a26608a26c0a");//审批用户2 lisi 加了myBean后

        taskService.complete("2e291336-855a-11f0-a950-a26608a26c0a");//审批用户3 wangwu
    }

}
